<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/07/24 14:00:35 $)
// $RCSfile: staff_search.php,v $ : $Revision: 1.12 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["t_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

define("CUSTOMFIELD_STAFFUSERTICKET",9);

require_once ("./includes/functions_html.php");
$grid = new Grid();
$template->loadLanguageSection("staff_tickets");
$template->loadLanguageSection("staff_tsearch");

require_once ("./modules/tickets/functions_ticketsearch.php");
require_once ("./modules/tickets/functions_ticketsui.php");
require_once ("./modules/tickets/functions_ticketcore.php");

/**
* ###############################################
* SEARCH ACTION
* ###############################################
*/
if ($eventaction == "search")
{
	if ($_GET["type"] == "newtickets")
	{
		$ticketsearchid = getNewTickets();
		header("location: index.php?_m=tickets&_a=manage&ticketsearchid=".intval($ticketsearchid));
		exit;
	} else if ($_REQUEST["type"] == "ticketid") {
		$_result = searchTicketID($_REQUEST["ticketid"]);

		if ($_result["type"] == SEARCH_TICKETID)
		{
			header("location: index.php?_m=tickets&_a=viewticket&ticketid=".intval($_result["ticketid"]));
			exit;
		} else if ($_result["type"] == SEARCH_TICKETIDLIST) {
			$_ticketsearchid = $_result["ticketsearchid"];
			header("location: index.php?_m=tickets&_a=manage&ticketsearchid=".intval($_ticketsearchid));
			exit;
		} else {
			header("location: index.php?_m=tickets&_a=manage&searchticketid=".urlencode($_REQUEST["ticketid"]));
			exit;
		}
	} else if ($_REQUEST["type"] == "query") {
		$_result = doTicketSearch($_REQUEST["query"]);
		if ($_result)
		{
			$_ticketsearchid = $_result;
			header("location: index.php?_m=tickets&_a=manage&ticketsearchid=".intval($_ticketsearchid));
			exit;
		} else {
			header("location: index.php?_m=tickets&_a=manage&searchquery=".urlencode($_REQUEST["query"]));
			exit;
		}
	} else if ($_REQUEST["type"] == "creator") {
		$_result = doMenuCreatorSearch($_REQUEST["fullname"]);
		if ($_result)
		{
			$_ticketsearchid = $_result;
			header("location: index.php?_m=tickets&_a=manage&ticketsearchid=".intval($_ticketsearchid));
			exit;
		} else {
			header("location: index.php?_m=tickets&_a=manage&searchquery=".urlencode($_REQUEST["fullname"]));
			exit;
		}
	} else if ($_POST["type"] == "advanced") {
		$_ticketsearchid = processSearchRules($_POST["searchrules"], $_POST["ruleoptions"]);
		if ($_ticketsearchid)
		{
			header("location: index.php?_m=tickets&_a=manage&ticketsearchid=".intval($_ticketsearchid));
			exit;
		} else {
			// Search Failed
			$infomessage = $_SWIFT["language"]["sanoresults"];
		}
	} else if (!empty($_GET["userid"])) {
		$_result = searchTicketsByUser($_GET["userid"], $_GET["ticketstatusid"]);
		if ($_result)
		{
			$_ticketsearchid = $_result;
			header("location: index.php?_m=tickets&_a=manage&ticketsearchid=".intval($_ticketsearchid));
			exit;
		} else {
			header("location: index.php?_m=tickets&_a=manage&searchquery=".urlencode($_GET["userid"]));
			exit;
		}
	} else if (!empty($_GET["usergroupid"])) {
		$_result = searchTicketsByUserGroup($_GET["usergroupid"], $_GET["ticketstatusid"]);
		if ($_result)
		{
			$_ticketsearchid = $_result;
			header("location: index.php?_m=tickets&_a=manage&ticketsearchid=".intval($_ticketsearchid));
			exit;
		} else {
			header("location: index.php?_m=tickets&_a=manage&searchquery=".urlencode($_GET["usergroupid"]));
			exit;
		}
	}


	/**
	* ###############################################
	* RENDER ADVANCED SEARCH FORM
	* ###############################################
	*/

		renderTicketTree();

		$template->assign("calendar", true);

		$interface->staffHeader($_SWIFT["language"]["ticket"] ." &gt; ".$_SWIFT["language"]["search"], 2);

		$interface->staffNavBar('<a href="index.php?_m=tickets&_a=manage" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=search" title="'.$_SWIFT["language"]["search"].'">'.$_SWIFT["language"]["search"]."</a>", $_SWIFT["language"]["asearchdesc"], 2);

		?>
		<script type="text/javascript">
			var cruleindex = 1;
			function newSearchRule()
			{
				var rowElement = document.createElement("div");
				rowElement.id = "rulerow"+cruleindex;
				var rowMod = cruleindex%2;
				rowElement.className = "searchrule"+rowMod;
				var tparent = browserObject("ruleparent");
				if (!tparent)
				{
					return false;
				}

				tparent.appendChild(rowElement);
				updateRuleHTML(cruleindex, "entiremessage");
				cruleindex++;
			}

			function removeRuleRow(ruleindex)
			{
				var tparent = browserObject("ruleparent");
				if (!tparent)
				{
					return false;
				}

				var rowElement = browserObject("rulerow"+ruleindex);
				if (!rowElement)
				{
					return false;
				}

				tparent.removeChild(rowElement);
			}

			function updateRuleHTML(ruleindex, fieldtype)
			{
				var returnResult;

				returnResult = '<table border="0" cellpadding="3" cellspacing="1" width="100%"><tr><td align="left" width="1"><a href="javascript:void(0);" onClick="javascript:removeRuleRow(\''+ruleindex+'\');"><img src="<?=$_SWIFT["themepath"]?>icon_deletesmall.gif" title="<?=$_SWIFT["language"]["deletecriteria"]?>" border="0" /></a></td><td align="left" width="1"><select name="searchrules['+ ruleindex +'][0]" class="swiftselect" onChange="javascript:updateRuleHTML(\''+ ruleindex +'\', this.options[this.selectedIndex].value);">';
				returnResult += '<optgroup label="<?=$_SWIFT["language"]["sticket"]?>">';
				returnResult += '<option value="entiremessage"'+selectedStatus(fieldtype, "entiremessage")+'><?=addslashes($_SWIFT["language"]["sentiremessage"])?></option>';
				returnResult += '<option value="subject"'+selectedStatus(fieldtype, "subject")+'><?=addslashes($_SWIFT["language"]["ssubject"])?></option>';
				returnResult += '<option value="fullname"'+selectedStatus(fieldtype, "fullname")+'><?=addslashes($_SWIFT["language"]["sfullname"])?></option>';
				returnResult += '<option value="email"'+selectedStatus(fieldtype, "email")+'><?=addslashes($_SWIFT["language"]["semail"])?></option>';
				returnResult += '<option value="groupname"'+selectedStatus(fieldtype, "groupname")+'><?=addslashes($_SWIFT["language"]["sgroupname"])?></option>';
				returnResult += '<option value="tgroupid"'+selectedStatus(fieldtype, "tgroupid")+'><?=addslashes($_SWIFT["language"]["stgroup"])?></option>';
				returnResult += '<option value="notes"'+selectedStatus(fieldtype, "notes")+'><?=addslashes($_SWIFT["language"]["snotes"])?></option>';
				returnResult += '<option value="usernotes"'+selectedStatus(fieldtype, "usernotes")+'><?=addslashes($_SWIFT["language"]["susernotes"])?></option>';
				returnResult += '<option value="departmentid"'+selectedStatus(fieldtype, "departmentid")+'><?=addslashes($_SWIFT["language"]["sdepartment"])?></option>';
				returnResult += '<option value="ticketstatusid"'+selectedStatus(fieldtype, "ticketstatusid")+'><?=addslashes($_SWIFT["language"]["sticketstatus"])?></option>';
				returnResult += '<option value="priorityid"'+selectedStatus(fieldtype, "priorityid")+'><?=addslashes($_SWIFT["language"]["spriority"])?></option>';
				returnResult += '<option value="ownerstaffid"'+selectedStatus(fieldtype, "ownerstaffid")+'><?=addslashes($_SWIFT["language"]["sowner"])?></option>';
				returnResult += '<option value="flagtype"'+selectedStatus(fieldtype, "flagtype")+'><?=addslashes($_SWIFT["language"]["sflagtype"])?></option>';
				returnResult += '<option value="slaplanid"'+selectedStatus(fieldtype, "slaplanid")+'><?=addslashes($_SWIFT["language"]["sslaplan"])?></option>';
				returnResult += '<option value="dateline"'+selectedStatus(fieldtype, "dateline")+'><?=addslashes($_SWIFT["language"]["screationdate"])?></option>';
				returnResult += '<option value="duetime"'+selectedStatus(fieldtype, "duetime")+'><?=addslashes($_SWIFT["language"]["sduetime"])?></option>';
				returnResult += '<option value="lastactivity"'+selectedStatus(fieldtype, "lastactivity")+'><?=addslashes($_SWIFT["language"]["slastactivity"])?></option>';
				returnResult += '<option value="lastuserreplytime"'+selectedStatus(fieldtype, "lastuserreplytime")+'><?=addslashes($_SWIFT["language"]["slastuserreplytime"])?></option>';
				returnResult += '<option value="laststaffreplytime"'+selectedStatus(fieldtype, "laststaffreplytime")+'><?=addslashes($_SWIFT["language"]["slaststaffreplytime"])?></option>';
				returnResult += '<option value="isphonecall"'+selectedStatus(fieldtype, "isphonecall")+'><?=addslashes($_SWIFT["language"]["sphoneticket"])?></option>';
				returnResult += '<option value="isescalated"'+selectedStatus(fieldtype, "isescalated")+'><?=addslashes($_SWIFT["language"]["sisescalated"])?></option>';
				returnResult += '<option value="hasattachments"'+selectedStatus(fieldtype, "hasattachments")+'><?=addslashes($_SWIFT["language"]["shasattachments"])?></option>';
				returnResult += '<option value="hasnotes"'+selectedStatus(fieldtype, "hasnotes")+'><?=addslashes($_SWIFT["language"]["shasnotes"])?></option>';
				returnResult += '<option value="isemailed"'+selectedStatus(fieldtype, "isemailed")+'><?=addslashes($_SWIFT["language"]["sisemailed"])?></option>';
				returnResult += '<option value="hasbilling"'+selectedStatus(fieldtype, "hasbilling")+'><?=addslashes($_SWIFT["language"]["shasbilling"])?></option>';
				returnResult += '</optgroup>';
				<?
				// First get the custom field groups
				$_customfieldgroups = $customfieldgroupidlist = $_customfields = $customfieldidlist = array();
				$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."customfieldgroups` WHERE `grouptype` IN ('". CUSTOMFIELD_USERTICKET ."', '". CUSTOMFIELD_STAFFTICKET ."', '". CUSTOMFIELD_STAFFUSERTICKET ."') ORDER BY `displayorder` ASC;");
				while ($dbCore->nextRecord())
				{
					$customfieldgroupidlist[]= $dbCore->Record["customfieldgroupid"];
					$_customfieldgroups[$dbCore->Record["customfieldgroupid"]] = $dbCore->Record;
					$_customfieldgroups[$dbCore->Record["customfieldgroupid"]]["fields"] = array();
				}

				if (count($customfieldgroupidlist))
				{
					$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."customfields` WHERE `customfieldgroupid` IN (". buildIN($customfieldgroupidlist) .");");
					while ($dbCore->nextRecord())
					{
						$customfieldidlist[] = $dbCore->Record["customfieldid"];
						$_customfields[$dbCore->Record["customfieldid"]] = $dbCore->Record;
						$_customfieldgroups[$dbCore->Record["customfieldgroupid"]]["fields"][$dbCore->Record["customfieldid"]] = &$_customfields[$dbCore->Record["customfieldid"]];
					}
				}

				foreach ($_customfieldgroups as $key=>$val)
				{
					?>
					returnResult += '<optgroup label="<?=addslashes($val["title"])?>">';
					<?
					foreach ($val["fields"] as $fieldkey=>$fieldval)
					{
						?>
						returnResult += '<option value="cfield_<?=$fieldval["customfieldid"]?>"'+selectedStatus(fieldtype, "cfield_<?=$fieldval["customfieldid"]?>")+'><?=addslashes($fieldval["title"])?></option>';
						<?
					}
					?>
					returnResult += '</optgroup>';
					<?
				}
				?>
				returnResult += '</select>';

				returnResult += "</td>";

				if (fieldtype == "entiremessage")
				{
					returnResult += getQueryHTML(ruleindex, "textentire");
				} else if (fieldtype == "subject") {
					returnResult += getOperatorHTML(ruleindex, "string")+getQueryHTML(ruleindex, "text");
				} else if (fieldtype == "contents") {
					returnResult += getOperatorHTML(ruleindex, "string")+getQueryHTML(ruleindex, "text");
				} else if (fieldtype == "departmentid") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "departmentid");
				} else if (fieldtype == "ticketstatusid") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "ticketstatusid");
				} else if (fieldtype == "priorityid") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "priorityid");
				} else if (fieldtype == "tgroupid") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "tgroupid");
				} else if (fieldtype == "ownerstaffid") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "ownerstaffid");
				} else if (fieldtype == "flagtype") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "flagtype");
				} else if (fieldtype == "isphonecall") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "bool");
				} else if (fieldtype == "isescalated") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "bool");
				} else if (fieldtype == "hasattachments") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "bool");
				} else if (fieldtype == "hasnotes") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "bool");
				} else if (fieldtype == "notes" || fieldtype == "email" || fieldtype == "fullname" || fieldtype == "groupname") {
					returnResult += getOperatorHTML(ruleindex, "resstring")+getQueryHTML(ruleindex, "text");
				} else if (fieldtype == "usernotes") {
					returnResult += getOperatorHTML(ruleindex, "resstring")+getQueryHTML(ruleindex, "text");
				} else if (fieldtype == "duetime") {
					returnResult += getOperatorHTML(ruleindex, "cal")+getQueryHTML(ruleindex, "cal");
				} else if (fieldtype == "lastactivity" || fieldtype == "lastuserreplytime" || fieldtype == "laststaffreplytime") {
					returnResult += getOperatorHTML(ruleindex, "cal")+getQueryHTML(ruleindex, "cal");
				} else if (fieldtype == "dateline") {
					returnResult += getOperatorHTML(ruleindex, "cal")+getQueryHTML(ruleindex, "cal");
				} else if (fieldtype == "slaplanid") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "slaplanid");
				} else if (fieldtype == "isemailed") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "bool");
				} else if (fieldtype == "hasbilling") {
					returnResult += getOperatorHTML(ruleindex, "bool")+getQueryHTML(ruleindex, "bool");
				}
				var cfield = fieldtype.substring(0,7);
				if (cfield == "cfield_")
				{
					returnResult += getOperatorHTML(ruleindex, "resstring")+getQueryHTML(ruleindex, "text");
				}

				var dRow = browserObject("rulerow"+ruleindex);
				if (!dRow)
				{
					return;
				}

				dRow.innerHTML = returnResult+'</tr></table>';
			}

			function selectedStatus(fieldtype, fieldvalue)
			{
				if (fieldtype == fieldvalue)
				{
					return " selected";
				}
			}

			function getOperatorHTML(ruleindex, optype)
			{
				if (!ruleindex)
				{
					return;
				}

				if (optype == "string")
				{
					return '<td align="left" width="1"><select name="searchrules['+ ruleindex +'][1]" class="swiftselect"><option value="<?=OP_CONTAINS?>" selected><?=$_SWIFT["language"]["opcontains"]?></option><option value="<?=OP_NOTCONTAINS?>"><?=addslashes($_SWIFT["language"]["opnotcontains"])?></option><option value="<?=OP_EQUAL?>"><?=$_SWIFT["language"]["opequal"]?></option><option value="<?=OP_NOTEQUAL?>"><?=$_SWIFT["language"]["opnotequal"]?></option></select></td>';
				} else if (optype == "resstring") {
					return '<td align="left" width="1"><select name="searchrules['+ ruleindex +'][1]" class="swiftselect"><option value="<?=OP_CONTAINS?>" selected><?=$_SWIFT["language"]["opcontains"]?></option></select></td>';
				} else if (optype == "bool") {
					return '<td align="left" width="1"><select name="searchrules['+ ruleindex +'][1]" class="swiftselect"><option value="<?=OP_EQUAL?>" selected><?=$_SWIFT["language"]["opequal"]?></option><option value="<?=OP_NOTEQUAL?>"><?=$_SWIFT["language"]["opnotequal"]?></option></select></td>';
				} else if (optype == "int") {
					return '<td align="left" width="1"><select name="searchrules['+ ruleindex +'][1]" class="swiftselect"><option value="<?=OP_GREATER?>"><?=$_SWIFT["language"]["opgreater"]?></option><option value="<?=OP_LESS?>"><?=$_SWIFT["language"]["opless"]?></option><option value="<?=OP_EQUAL?>" selected><?=$_SWIFT["language"]["opequal"]?></option><option value="<?=OP_NOTEQUAL?>"><?=$_SWIFT["language"]["opnotequal"]?></option></select></td>';
				} else if (optype == "cal") {
					return '<td align="left" width="1"><select name="searchrules['+ ruleindex +'][1]" class="swiftselect"><option value="<?=OP_GREATER?>" selected><?=$_SWIFT["language"]["opgreater"]?></option><option value="<?=OP_LESS?>"><?=$_SWIFT["language"]["opless"]?></option></select></td>';
				}
				
			}

			function getQueryHTML(ruleindex, querytype)
			{
				if (!ruleindex)
				{
					return;
				}

				var returnResult = '<td align="left">';
				if (querytype == "text")
				{
					returnResult += '<input type="text" size="20" class="swifttext" name="searchrules['+ ruleindex +'][2]" style="width:98%;" />';
				} else if (querytype == "textentire") {
					returnResult += '<input type="text" size="20" class="swifttext" name="searchrules['+ ruleindex +'][2]" style="width:50%;" /><span class="smalltext">&nbsp;&nbsp;<?=addslashes($_SWIFT["language"]["squerydesc"])?></span>';
				} else if (querytype == "ticketstatusid") {
					returnResult += '<select name="searchrules['+ ruleindex +'][2]" class="swiftselect">';
					<?
					foreach ($_SWIFT["statuscache"] as $key=>$val)
					{
						?>returnResult += '<option value="<?=$val["ticketstatusid"]?>" style="color:<?=$val["statuscolor"]?>;"><?=addslashes($val["title"])?></option>';<?
					}
					?>
					returnResult += '</select>';
				} else if (querytype == "tgroupid") {
					returnResult += '<select name="searchrules['+ ruleindex +'][2]" class="swiftselect">';
					<?
					foreach ($_SWIFT["tgroupcache"] as $key=>$val)
					{
						?>returnResult += '<option value="<?=$val["tgroupid"]?>"><?=addslashes($val["title"])?></option>';<?
					}
					?>
					returnResult += '</select>';
				} else if (querytype == "priorityid") {
					returnResult += '<select name="searchrules['+ ruleindex +'][2]" class="swiftselect">';
					<?
					foreach ($_SWIFT["prioritycache"] as $key=>$val)
					{
						?>returnResult += '<option value="<?=$val["priorityid"]?>" style="color:<?=$val["frcolorcode"]?>;background:<?=$val["bgcolorcode"]?>;"><?=addslashes($val["title"])?></option>';<?
					}
					?>
					returnResult += '</select>';
				} else if (querytype == "ownerstaffid") {
					returnResult += '<select name="searchrules['+ ruleindex +'][2]" class="swiftselect">';
					<?
					foreach ($_SWIFT["staffcache"] as $key=>$val)
					{
						?>returnResult += '<option value="<?=$val["staffid"]?>"><?=addslashes($val["fullname"])?></option>';<?
					}
					?>
					returnResult += '</select>';
				} else if (querytype == "departmentid") {
					returnResult += '<select name="searchrules['+ ruleindex +'][2]" class="swiftselect">';
					<?
					$_assigns = getAssignedDepartments(MODULE_TICKETS);
					foreach ($_assigns as $key=>$val)
					{
						$_department = $_SWIFT["departmentcache"][$val];
						?>returnResult += '<option value="<?=$_department["departmentid"]?>"><?=addslashes($_department["title"])?></option>';<?
					}
					?>
					returnResult += '</select>';
				} else if (querytype == "slaplanid") {
					returnResult += '<select name="searchrules['+ ruleindex +'][2]" class="swiftselect">';
					<?
					foreach ($_SWIFT["slaplancache"] as $key=>$val)
					{
						?>returnResult += '<option value="<?=$val["slaplanid"]?>"><?=addslashes($val["title"])?></option>';<?
					}
					?>
					returnResult += '</select>';
				} else if (querytype == "cal") {
					returnResult += '<input type="text" name="searchrules['+ruleindex+'][2]" id="rulecal'+ ruleindex +'" onclick="return showCalendar(\'rulecal'+ ruleindex +'\', \'<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>\');" size="12" readonly="1" value="" class="swifttext"/>&nbsp;<img src="<?=$_SWIFT["themepath"]?>calendar.gif" onclick="return showCalendar(\'rulecal'+ ruleindex +'\', \'<?=iif($_SWIFT["settings"]["dt_caltype"]=="us", "%m/%d/%Y", "%d/%m/%Y")?>\');" style="cursor: pointer;" align="absmiddle"/>';
				} else if (querytype == "bool") {
					returnResult += '<select name="searchrules['+ ruleindex +'][2]" class="swiftselect"><option value="1" selected><?=$_SWIFT["language"]["strue"]?></option><option value="0"><?=$_SWIFT["language"]["sfalse"]?></option></select>';
				} else if (querytype == "flagtype") {
					<?
					$flagbgcolors = array(FLAG_PURPLE => "#A5587C", FLAG_ORANGE => "#FF8C5A", FLAG_GREEN => "#8BB467", FLAG_YELLOW => "#FFC160", FLAG_RED => "#CF5D60", FLAG_BLUE => "#5C83B4");
					?>
					returnResult += '<select name="searchrules['+ ruleindex +'][2]" class="swiftselect">';
					returnResult += '<option value="<?=FLAG_PURPLE?>" style="background: <?=$flagbgcolors[FLAG_PURPLE]?>; color: #FFFFFF;" selected><?=addslashes($_SWIFT["language"]["purpleflag"])?></option>';
					returnResult += '<option value="<?=FLAG_RED?>" style="background: <?=$flagbgcolors[FLAG_RED]?>; color: #FFFFFF;"><?=addslashes($_SWIFT["language"]["redflag"])?></option>';
					returnResult += '<option value="<?=FLAG_ORANGE?>" style="background: <?=$flagbgcolors[FLAG_ORANGE]?>; color: #FFFFFF;"><?=addslashes($_SWIFT["language"]["orangeflag"])?></option>';
					returnResult += '<option value="<?=FLAG_YELLOW?>" style="background: <?=$flagbgcolors[FLAG_YELLOW]?>; color: #FFFFFF;"><?=addslashes($_SWIFT["language"]["yellowflag"])?></option>';
					returnResult += '<option value="<?=FLAG_BLUE?>" style="background: <?=$flagbgcolors[FLAG_BLUE]?>; color: #FFFFFF;"><?=addslashes($_SWIFT["language"]["blueflag"])?></option>';
					returnResult += '<option value="<?=FLAG_GREEN?>" style="background: <?=$flagbgcolors[FLAG_GREEN]?>; color: #FFFFFF;"><?=addslashes($_SWIFT["language"]["greenflag"])?></option>';
					returnResult += '<option value="0" style="background: #CCCCCC; color: #FFFFFF;"><?=addslashes($_SWIFT["language"]["noflag"])?></option>';
					returnResult += '</select>';
				}

				return returnResult+'</td>';
			}
		</script>
		<?


		printInfoBox($infomessage);
		printErrorBox($errormessage);

		printFormStart();
		printMainTableHeader($_SWIFT["language"]["searchrules"]);

		echo '<tr class="'. getRowBG() .'">
		<td align="left" colspan="2"><span class="smalltext"><div id="ruleparent"></div></span></td></tr>';

		echo '<tr class="'. getRowBG() .'">
		<td align="left" colspan="2"><span class="smalltext"><a href="javascript:newSearchRule();"><img src="'. $_SWIFT["themepath"] .'icon_add.gif" border="0" align="absmiddle" />&nbsp;'. $_SWIFT["language"]["newrule"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label for="ruleall"><input type="radio" name="ruleoptions" value="all" id="ruleall" checked>'. $_SWIFT["language"]["smatchall"] .'</label>&nbsp;<label for="ruleany"><input type="radio" name="ruleoptions" value="any" id="ruleany">'. $_SWIFT["language"]["smatchany"] .'</label></span></td></tr>';

		printSubmitRow($_SWIFT["language"]["search"]);

		printMainTableFooter();

		addHiddenField("_m", "tickets");
		addHiddenField("_a", "search");
		addHiddenField("type", "advanced");
		printFormEnd();


		?>
		<script type="text/javascript">
			newSearchRule();
		</script>
		<?
		$template->assign("backurl", "index.php?_m=tickets&_a=manage");
		$interface->staffFooter();
}
?>